#include <iostream>
#include <cassert>
#include <fstream>
//#include <string>
#include "include/Matrix.h"
#include "include/IntArray.h"
#include "include/MyArray.h"

using std::cout;
using std::endl;
using std::string;

void fileTest();
void testIntArray();
void testMatrix();
void testMyArray();
void testSomething();
void test_ref(int &);
void test_pnt(const int *);
void test_string();
void test_const_pt();
void test_const();
void test_pp(int **);
void test_pp1(); //指针的指针
void test_rp(); //int *&, 指针的引用
int main()
{
    //testIntArray();
    //testMatrix();
    //testMyArray();
    //test_string();
    //test_const_pt();
    //testSomething();
    //test_const();

    //test_pp(p);
    test_rp();

    /*{int int_1 = 100;
    int int_2 = 323;
    int *pi = &int_1;
    cout << "pi = " << pi << endl;
    pi = &int_2;
    cout << "pi = " << pi << endl;
    cout << "-------" << endl;
    int value = 288;
    int *i = &value;
    cout << "i addr = " << i << endl;
    test_pnt(i);
    cout << "after test_pnt(const int *), value = " << value << endl;
    cout << "after test_pnt(const int *) = " << *i << ", i addr = " << i << endl;
    }*/
    return 0;
}
void fileTest()
{
   cout << "file testing" << endl;
    ofstream outfile("out_file");
    ifstream infile("in_file");
    if (!infile) {
        cerr << "error: can't open input file\n";
        //return -1;
    }
    if (!outfile) {
        cerr << "error: can't open output file\n";
        //return -2;
    }
    string word;
    while (cin >> word) {
        cout << "word is " << word << endl;
    }
    cout << "no more words";
}

void testIntArray()
{
    int a[6] = {1, 3, 5, 7, 9, 15};
    int *array = a;
    IntArray ia(array, 6);
    cout << "size = " << ia.size() << endl;

    cout << 'A' << 'b' << endl;
    cout << L"A" << 'b' << endl;
    cout << ia[3] << endl;
    ia[3] = 189;
    cout << ia[3] << endl;
    cout << "-------------" << endl;
    IntArray ia_1;
    ia_1 = ia;
    ia_1[2] = 892;
    cout << "ia_1 size = " << ia_1.size() << endl;
    if (ia == ia_1)
    {
        cout << "ia = ia_1" << endl;
    } else {
        cout << "ia != ia_1" << endl;
    }
    //cout << "  ia & = " << &ia << endl;
    //cout << "ia_1 & = " << &ia_1 << endl;
}

void testMatrix()
{
    int ia[6] = {2, 4, 6, 8, 12, 36};
    int *array = ia;
    Matrix m(2, 3, array);
    cout << "row = " << m.getRowSize() << ", col = " << m.getColSize() << endl;
    cout << m(0, 1) << endl;
    Matrix m_1;
    m_1 = m;
    cout << "m_1.size = " << m_1.getColSize() << endl;
}

/*void testMyArray()
{
    const int ai_size = 6;
    MyArray<int> ai(ai_size);
    int i = 0;
    for (i = 0; i < ai_size; i++)
    {
        ai[i] = i++;
    }
    for (i = 0; i < ai_size; i++)
    {
        cout << "ai[" << (i + 1) << "] = " << ai[i] << endl;
    }
}*/

void testSomething()
{

    int *pi;
    int val = 12;
    pi = &val;
    int **ppi = &pi;
    int **ppi2;
    ppi2 = &pi;
    cout << "    *pi = " << *pi << endl;
    cout << "  **ppi = " << **ppi << endl;
    cout << "**ppi2 = " << **ppi2 << endl;

    int value_2 = 2048;
    int *pi2 = &value_2;
    int **pi3 = 0;
    pi3 = &pi2;
    cout << "**pi3 = " << **pi3 << endl;
}

void test_pnt(const int *i)
{
    cout << "test_pnt, i addr = " << i << endl;
    cout << "test * i = " << *i << endl;
    cout << "then i change the value *i " << endl;
    int d = 334;
    cout << "d addr = " << &d << endl;
    i = &d;
    cout << "test_pnt i = " << i << endl;
    cout << "test * i = " << *i << endl;
}

void test_ref(int &i)
{
    cout << "test & i = " << i << endl;
    cout << "then i change the value of i " << endl;
    i = 999;
    cout << "test & i = " << i << endl;
}

void test_string()
{
    string str("string1");
    string str1 = str;
    cout << " &str = " << &str << endl;
    cout << "&str1 = " << &str1 << endl;
}

void test_const_pt()
{
    int i = 199;
    int i_1 = 83293;
    const int ci = i;
    const int *cpi = &i;
    cout << "*cpi = " << *cpi << endl;
    cout << "----" << endl;
    cpi = &i_1;
    cout << "*cpi = " << *cpi << endl;
}

void test_const()
{
    int i = 3234432;
    const int *pi = &i;
    cout << *pi << endl;
    int i_1 = 7131;
    pi = &i_1;
    cout << *pi << endl;
}

void test_pp(int **p)
{
    int *pp = new int;
    *p = pp;
    cout << "  p = " << p << endl;
    cout << " *p = " << *p << endl;
    cout << " &p = " << &p << endl;
    cout << "**p = " << **p << endl;
}

void test_pp1()
{
    int i = 12;

    int *pt = &i;
    cout << "*pt = "<< *pt << ", 指针pt所存储的地址值 = "<< pt << ", 指针pt的地址&pt = " << &pt
            << ", i = " << &i << endl;
    //int **p = &pt;
    int **p;
    *p = pt;
    cout << "&pt = " << &pt << ", **p = " << **p << ", *p = " << *p << ", p = " << p
        << ", &p = " << &p << endl;
}

void test_rp()
{
    int i = 1234;
    int *p = &i;
    int *&rp = p;
    cout << "i = " << i << ", &i = " << &i << endl;
    cout << "*p = " << *p << ", p = " << p << ", &p = " << &p <<endl;
    cout << "rp = " << rp << ", *rp = " << *rp << ", &rp = " << &rp << endl;
}
